package com.example.petmall.seckill.schdeuling;

import com.example.petmall.seckill.constant.SeckillConstant;
import com.example.petmall.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author liang zai
 * @date 2022/5/23 18:13
 * @description
 */
@Service
@Slf4j
public class SeckillSchedule {

    @Autowired
    private SeckillService seckillService;

    @Autowired
    private RedissonClient redissonClient;

    //TODO 秒杀任务的幂等性问题
    @Scheduled(cron = "0 * * * * ?")
    public void upload3Day(){
        log.info("商品上架成功.....");
        RLock lock = redissonClient.getLock(SeckillConstant.SESSION_CACHE_LOCK);
        try {
            lock.lock(10, TimeUnit.SECONDS);
            seckillService.upload3Day();
        }finally {
            lock.unlock();
        }

    }
}
